﻿#region header

//  ***************************************************************************
// *                                LICENSE
//  ***************************************************************************
//  * "Haplous" .NET Simple 2D Game Framework
//  * 
//  * Copyright © Jonathan Evans 2014-2014
//  * 
//  * This program is free software; you can redistribute it and/or
//  * modify it under the terms of the GNU General Public License
//  * as published by the Free Software Foundation; either version 2
//  * of the License, or (at your option) any later version.
//  * 
//  * This program is distributed in the hope that it will be useful,
//  * but WITHOUT ANY WARRANTY; without even the implied warranty of
//  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  * GNU General Public License for more details.
//  * 
//  * You should have received a copy of the GNU General Public License
//  * along with this program; if not, write to the Free Software
//  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//  * 
//  ***************************************************************************
//  * 
//  * Contact: opencollar@googlemail.com
//  * 
//  * Website: http://haplous.codeplex.com/
//  *
//  * NuGet:   http://www.nuget.org/packages/haplous/
//  *
//  * File:    Haplous/Haplous/BrushConverter.cs
//  *
//  ***************************************************************************

#endregion

using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

using JetBrains.Annotations;

namespace Haplous.Presentation.Converters
{
    /// <summary>
    ///     A converter that converts between <see cref="Color" /> and <see cref="SolidColorBrush" /> types.
    /// </summary>
    public class BrushConverter : IValueConverter
    {
        /// <summary>
        ///     Converts a value.
        /// </summary>
        /// <returns>
        ///     A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1")]
        public object Convert([CanBeNull] object value, [NotNull] Type targetType, [CanBeNull] object parameter, [CanBeNull] CultureInfo culture)
        {
            if((targetType != typeof(Brush)) && (targetType != typeof(SolidColorBrush)) && targetType.IsAssignableFrom(typeof(Brush)))
                return null;

            var gameColor = value as Color;
            if(gameColor != null)
                return new SolidColorBrush(System.Windows.Media.Color.FromArgb(gameColor.Alpha, gameColor.Red, gameColor.Green, gameColor.Blue));

            var mediaColor = value as System.Windows.Media.Color?;
            if(mediaColor != null)
                return new SolidColorBrush((System.Windows.Media.Color)mediaColor);

            var drawingColor = value as System.Drawing.Color?;
            if(drawingColor != null)
            {
                var c = (System.Drawing.Color)drawingColor;
                return new SolidColorBrush(System.Windows.Media.Color.FromArgb(c.A, c.R, c.G, c.B));
            }

            return null;
        }

        /// <summary>
        ///     Converts a value.
        /// </summary>
        /// <returns>
        ///     A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}